package com.pccw.usercentercontroller.vo;

import com.pccw.usercenterservice.exception.PccwException;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;

import javax.validation.constraints.NotBlank;
import java.util.regex.Pattern;

@Data
public class UserRegisterVO {

    /**
     * 电子邮箱
     */
    @NotBlank(message = "email不能为空")
    private String email;

    /**
     * 密码（前端加密后）
     */
    @NotBlank(message = "密码不能为空")
    private String passWord;

    /**
     * 用户名
     */
    @NotBlank(message = "用户名不能为空")
    private String userName;

    public  void checkValid(){
        if(StringUtils.isNotBlank(this.email)){
                boolean matches = Pattern.matches("^(\\w+([-.][A-Za-z0-9]+)*){3,18}@\\w+([-.][A-Za-z0-9]+)*\\.\\w+([-.][A-Za-z0-9]+)*$", email);
                if(!matches){
                    throw new PccwException("email格式有误");
                }
        }
        if(StringUtils.isNotBlank(this.userName)){
            if(this.userName.length()>=30){
                throw new PccwException("用户名过长，请修改后重试");
            }
        }
    }
}
